package com.mxnet.chongjin.supoman.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.activity.AboutActivity;
import com.mxnet.chongjin.supoman.activity.AppointTimeActivity;
import com.mxnet.chongjin.supoman.activity.ChangeLanguageActivity;
import com.mxnet.chongjin.supoman.activity.FirmwareUpgradeActivity;
import com.mxnet.chongjin.supoman.activity.RepairRecordActivity;
import com.mxnet.chongjin.supoman.activity.SecurityCodeActivity;
import com.mxnet.chongjin.supoman.activity.SetAppointmentActivity;
import com.mxnet.chongjin.supoman.activity.UserInfoActivity;
import com.mxnet.chongjin.supoman.activity.UserLoginActivity;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.style.CircleImageView;
import com.mxnet.chongjin.supoman.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment implements View.OnClickListener {
    private Context mContext;
    private CircleImageView mImageView;
    private TextView mTextView;
    private FrameLayout mLayout1, mLayout2, mLayout3, mLayout4, mLayout5, mLayout6, mLayout7;
    private String version;
    private SharedPreferences mPreferences;

    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_mine, container, false);
        mContext = getContext();
        mPreferences = getActivity().getSharedPreferences("supoman", Context.MODE_PRIVATE);
        initView(v);
        return v;
    }

    public void initView(View view) {
        mImageView = view.findViewById(R.id.iv_mine);
        if (mPreferences.getString("userImgUrl", "").equals("")) {
            mImageView.setImageResource(R.mipmap.mine_head_default2);
        } else {
            Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + mPreferences.getString("userImgUrl", "")).into(mImageView);
        }
        mImageView.setOnClickListener(this);

        mTextView = view.findViewById(R.id.tv_mine);
        if (mPreferences.getString("userName", "").equals("")) {
            mTextView.setText(getText(R.string.login_and_regist));
        } else {
            mTextView.setText(mPreferences.getString("userName", ""));
        }
        mTextView.setOnClickListener(this);

        mLayout1 = view.findViewById(R.id.fl_mine_set_appointment);
        mLayout1.setOnClickListener(this);

        mLayout2 = view.findViewById(R.id.fl_mine_appoint_time);
        mLayout2.setOnClickListener(this);

        mLayout3 = view.findViewById(R.id.fl_mine_security_code);
        mLayout3.setOnClickListener(this);

        mLayout4 = view.findViewById(R.id.fl_mine_repair_record);
        mLayout4.setOnClickListener(this);

        mLayout5 = view.findViewById(R.id.fl_mine_firmware_upgrade);
        mLayout5.setOnClickListener(this);

        mLayout6 = view.findViewById(R.id.fl_mine_change_language);
        mLayout6.setOnClickListener(this);

        mLayout7 = view.findViewById(R.id.fl_mine_about);
        mLayout7.setOnClickListener(this);
    }

    @Override
    public void onClick(final View v) {
        final Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.iv_mine:
                if (!mPreferences.getBoolean("isLogin", false)) {
                    ToastUtils.showToast(mContext, getResources().getString(R.string.login_first));
                } else {
                    intent.setClass(mContext, UserInfoActivity.class);
                    startActivityForResult(intent, 55);
                }
                break;
            case R.id.tv_mine:
                if (!mPreferences.getBoolean("isLogin", false)) {
                    intent.setClass(mContext, UserLoginActivity.class);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.fl_mine_set_appointment:
                intent.setClass(mContext, SetAppointmentActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.fl_mine_appoint_time:
                intent.setClass(mContext, AppointTimeActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.fl_mine_security_code:
                intent.setClass(mContext, SecurityCodeActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.fl_mine_repair_record:
                if (!mPreferences.getBoolean("isLogin", false)) {
                    intent.setClass(mContext, UserLoginActivity.class);
                    mContext.startActivity(intent);
                } else {
                    intent.setClass(mContext, RepairRecordActivity.class);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.fl_mine_firmware_upgrade:
                OkHttpUtils.get()
                        .url(Api.URL_HEADER + Api.FIRMWARE_VERSION)
                        .build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                boolean success = jsonObject.optBoolean("success");
                                if (success) {
                                    JSONObject data = jsonObject.optJSONObject("data");
                                    version = data.optString("versionNo");
                                    intent.setClass(mContext, FirmwareUpgradeActivity.class);
                                    intent.putExtra("version", version);
                                    mContext.startActivity(intent);
                                } else {
                                    ToastUtils.showToast(mContext, "获取最新版本失败！");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                break;
            case R.id.fl_mine_change_language:
                intent.setClass(mContext, ChangeLanguageActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.fl_mine_about:
                intent.setClass(mContext, AboutActivity.class);
                mContext.startActivity(intent);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 55 && resultCode == 56) {
            Glide.with(mContext).load(Api.URL_HEADER + Api.GET_IMAGE + data.getStringExtra("userHead")).into(mImageView);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
